package com.chenfwind.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.chenfwind.common.Constant;
import com.chenfwind.common.ResultVO;
import com.chenfwind.common.StringUtils;
import com.chenfwind.domain.Comment;
import com.chenfwind.domain.Friend;
import com.chenfwind.domain.FriendInvitation;
import com.chenfwind.domain.vo.BookVO;
import com.chenfwind.domain.vo.CommentVO;
import com.chenfwind.domain.vo.FriendVO;
import com.chenfwind.domain.vo.InvitationVO;
import com.chenfwind.service.BookService;
import com.chenfwind.service.FriendService;
import com.chenfwind.service.InvitationService;
import com.chenfwind.service.UserService;

@Controller
@RequestMapping("/friend")
public class FriendInvationController {
	
	@Autowired
	private InvitationService invitationService;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private FriendService friendService;
	/*
	 * invitation 的参数有  UID1,UID2,STATUS
	 */
	@ResponseBody
	@RequestMapping(value = "/invitationFriend", method = RequestMethod.POST)
	public ResultVO<String> invitationFriend(HttpSession session,
			@RequestParam(value = "invitationInfoJson", required = false) String invitationInfoJson ) throws Exception {
		ResultVO<String> vo = ResultVO.create();
		InvitationVO invitionVO = JSON.parseObject(invitationInfoJson, InvitationVO.class);
		if (invitionVO == null) {
			return ResultVO.create(Constant.CODE_NOPARAM, Constant.CODE_MSG_NOPARAM);
		}
		FriendInvitation invitation = FriendInvitationVO2FriendInvitation(invitionVO , null);
		invitation.setCreateTime(new Date());
		invitationService.save(invitation);
		vo.setStatus(Constant.CODE_SUCCESS);;
		return vo;
	}
	
	public FriendInvitation FriendInvitationVO2FriendInvitation(InvitationVO invitationVO , FriendInvitation invitation ){
		if (invitation == null) {
			invitation = new FriendInvitation();
			invitation.setId(StringUtils.genUUID());
			invitation.setInviteStatus(0);
		}
		
		com.chenfwind.utils.BeanUtils.copyProperties(invitationVO, invitation);
		return invitation;
	}
	
	@ResponseBody
	@RequestMapping(value = "/changeInvitation", method = RequestMethod.POST)
	public ResultVO<String> changeInvitation(HttpSession session,
			@RequestParam(value = "invitationId", required = false) String invitationId ,
			@RequestParam(value = "status" ,required = false) String status) throws Exception {
		ResultVO<String> vo = ResultVO.create();
		if (invitationId == null || status == null) {
			return ResultVO.create(Constant.CODE_NOPARAM, Constant.CODE_MSG_NOPARAM);
		}
		FriendInvitation invitaion = invitationService.findInvitationById(invitationId);
		if (invitaion == null) {
			return ResultVO.create(Constant.CODE_NOPARAM, Constant.CODE_MSG_NOPARAM);
		}
		Friend friend = friendService.findFriendByUid1AndUid2(invitaion.getInviteeUserId(), invitaion.getInviterUserId());
		if (friend != null) {
			return ResultVO.create(Constant.CODE_NOPARAM, "已经成为好友了");
		}
		invitationService.changeInvitationStatus(invitaion,status);
		vo.setStatus(Constant.CODE_SUCCESS);;
		return vo;
	}
	
	@ResponseBody
	@RequestMapping(value = "/getAllInvitation", method = RequestMethod.POST)
	public ResultVO<List<InvitationVO>> getAllInvitation(HttpSession session,
			@RequestParam(value = "userId", required = false) String userId) throws Exception {
		ResultVO<List<InvitationVO>> vo = ResultVO.create();
		if (userId == null) {
			return ResultVO.create(Constant.CODE_NOPARAM, Constant.CODE_MSG_NOPARAM);
		}
		List<FriendInvitation> invitationList = invitationService.findInvitationByUid(userId);
		List<InvitationVO> invitationVOList = new ArrayList<InvitationVO>();
		for (FriendInvitation invitation : invitationList) {
			invitationVOList.add(Invitation2InvitationVO(invitation , null));
		}
		vo.setResult(invitationVOList);
		vo.reCalcStatus();
		return vo;
	}
	
	public InvitationVO Invitation2InvitationVO(FriendInvitation invitation , InvitationVO invitationVO){
		if (invitationVO == null) {
			invitationVO = new InvitationVO();
			invitationVO.setInviteeUserName(userService.findUserById(invitation.getInviteeUserId()).getNickname());
			invitationVO.setInviterUserName(userService.findUserById(invitation.getInviterUserId()).getNickname());
		}
		com.chenfwind.utils.BeanUtils.copyProperties(invitation, invitationVO);
		return invitationVO;
	}
	
	@ResponseBody
	@RequestMapping(value = "/getAllFriend", method = RequestMethod.POST)
	public ResultVO<List<FriendVO>> getAllFriend(HttpSession session,
			@RequestParam(value = "userId", required = false) String userId) throws Exception {
		ResultVO<List<FriendVO>> vo = ResultVO.create();
		if (userId == null) {
			return ResultVO.create(Constant.CODE_NOPARAM, Constant.CODE_MSG_NOPARAM);
		}
		List<Friend> friendList = friendService.getAllFriend(userId);
		List<FriendVO> friendVOList = new ArrayList<FriendVO>();
		for (Friend friend : friendList) {
			friendVOList.add(Friend2FriendVO(friend , null));
		}
		vo.setResult(friendVOList);
		vo.setStatus(Constant.CODE_SUCCESS);
		return vo;
	}
	
	public FriendVO Friend2FriendVO(Friend friend,FriendVO friendVO){
		if (friendVO == null) {
			friendVO = new FriendVO();
			friendVO.setUser1Name(userService.findUserById(friend.getUser1Id()).getNickname());
			friendVO.setUser2Name(userService.findUserById(friend.getUser2Id()).getNickname());
		}	
		com.chenfwind.utils.BeanUtils.copyProperties(friend, friendVO);
		return friendVO;
	}
	
	
}
